package main

import (
	"fmt"
)

type Count struct {
	total   float64 // 总金额
	money   float64 // 收支
	content string  // 说明
	balance string  // 收入  支出
}

func (c *Count) String() string {
	return fmt.Sprintf("%s\t %.2f\t\t %.2f\t\t %s", c.balance, c.total, c.money, c.content)
}

func (c *Count) Earning(content string, balance string, money float64) {
	c.content = content
	c.balance = balance
	c.money = money
	c.total += money
}

func (c *Count) Pay(content string, balance string, money float64) {
	if c.total < money {
		fmt.Println("账户余额不足....")
		return
	}
	c.content = content
	c.balance = balance
	c.money = money
	c.total -= c.money
}

func main() {
	c := &Count{}

	for loop := true; loop; {
		fmt.Println("----------------家庭收支记账软件-----------------")
		fmt.Println("                 1. 收支明细                    ")
		fmt.Println("                 2. 登记收入                    ")
		fmt.Println("                 3. 登记登出                    ")
		fmt.Println("                 4. 退出                       ")
		fmt.Printf("  请选择（1-4）： ")
		var num byte
		fmt.Scanf("%d", &num)
		switch num {
		case 1:
			fmt.Println("----------当前收支明细记录--------------")
			fmt.Println("收支\t账户金额\t\t收支金额\t\t说明")
			if c.total <= 0.0 {
				fmt.Println("当前没有任何收支明细，请来一笔吧.....")
				break
			}
			fmt.Println(c)

		case 2:
			var money float64
			var content string
			var balance string
			fmt.Printf("本次收入金额:  ")
			fmt.Scanf("%f\n", &money)
			fmt.Printf("说明：")
			fmt.Scanf("%s\n", &content)
			balance = "收入"
			c.Earning(content, balance, money)
		case 3:
			var money float64
			var content string
			var balance string
			fmt.Printf("本次支出金额:  ")
			fmt.Scanf("%f\n", &money)
			fmt.Printf("说明：")
			fmt.Scanf("%s\n", &content)
			balance = "支出"
			c.Pay(content, balance, money)
		case 4:
			fmt.Println("您确认退出吗? (y/n)")
			var ex byte
			for {
				fmt.Scanf("%c\n", &ex)
				if ex == 'y' || ex == 'n' {
					break
				}
				fmt.Println("您输入有误，请重新输入:(y/n)")
			}
			if ex == 'y' {
				loop = !loop
				fmt.Println("已退出")
			}
		default:
			fmt.Println("请输入正确功能...")

		}
	}
}
